<?php

class Application_Model_CommentService extends Base_Service
{
   
	public function create($comment) {		
		$db = $this->getDbAdapter();
		
		$row = array ( 
			'content_id' => $comment['contentId'],
			'content_type' => $comment['contentType'],
			'uid' => $comment['userId'],
			'comment' => $comment['comment'],
			'create_date' => date('Y-m-d H:i:s'),
			'status' => Application_Model_Constants::STATUS_NORMAL,
		);
		$this->logger->debug(__METHOD__. '   ' .$row);		
		$rows_affected = $db->insert('comments', $row);
		$last_insert_id = $db->lastInsertId();
		$this->logger->debug(__METHOD__. '   ' .$last_insert_id);
		
		return $last_insert_id;
		
	}
	/**
	 * 
	 * get comments for content
	 * @param unknown_type $contentType
	 * @param unknown_type $contentId
	 */
	public function getComments($contentType, $contentId){
		$db = $this->getDbAdapter();
		$sql = "select a.*, b.nickname"
		." from comments a, user b"
		." where a.uid = b.uid and a.content_type=".$contentType." and a.content_id=".$contentId;
		$this->logger->debug(__METHOD__." sql: ".$sql);
		return $db->fetchAll($sql);
	}
	
	public function getAllComments($uid) {
		$db = $this->getDbAdapter();
		
		//sql for comments of blogs
		$sql = "select a.*, b.[uid] content_owner_id, b.[nickname] content_owner_nickname"
		.", c.[uid] comment_owner_id, c.[nickname] commnet_owner_nickname"
		.", d.[title] content_title"
		." from comments a, user b, user c , blog d"
		." where a.[content_type] = ".Application_Model_Constants::CONTENT_TYPE_BLOG
		." and a.[content_id] = d.[bid] and b.[uid] = d.[owner_id] and a.[uid]= c.[uid]";
		
		if ($uid) {
			$sql = $sql ." and c.[uid]=".$uid;
		}
		
		$sql = $sql ." order by a.[create_date] desc limit 20";
		$this->logger->debug(__METHOD__." sql: ".$sql);
		return $db->fetchAll($sql);
	}
	
	public function deleteComment($uid, $commentId) {
	}
	
	
	protected static $_instance = null;
	public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}

